package com.example.unisys.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

public class LivenessUtil {
    public static String apikey="D7fjkjGwYaaCcSG2zOyYLqF4";

    public static String secretkey="RiOGQhzLauj5ZMNZoSpr8UrtlVe779me";

    public static String baseurl="https://aip.baidubce.com/oauth/2.0/token";

    public static String paramsp="&";
    public static String getToken(){
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        map.add("grant_type","client_credentials");
        map.add("client_id",apikey);
        map.add("client_secret",secretkey);
        //HttpEntity<Map<String,Object>> requestEntity = new HttpEntity<>(map, headers);
        //ResponseEntity<String> response = restTemplate.exchange(baseurl,HttpMethod.POST,requestEntity,String.class);
        String result=restTemplate.postForObject(baseurl,map,String.class);
        System.out.println(result);
        HashMap<String, Object> hashMap= JSON.parseObject(result, HashMap.class);
        System.out.println(hashMap.get("access_token"));
        if(hashMap.containsKey("error"))
            return "";
        else
            return (String)hashMap.get("access_token");

    }

    public static double getscore(String res){
        int pos1=res.indexOf("score");
        if(pos1<0)
            return -1.0;
        else{
            String tmpstring=res.substring(pos1,res.length());
            int pos2=tmpstring.indexOf(",");
            //System.out.println(pos1);
            //System.out.println(pos2);
            String score=tmpstring.substring(6,pos2);
            System.out.println(score.trim());
            return Double.parseDouble(score.trim());
        }
    }

    public static String getpic(String res){
        int pos1=res.indexOf("\"pic\"");
        if(pos1<0){
            return null;
        }
        else{
            String tmpstring=res.substring(pos1+8,res.length());
            int pos2=tmpstring.indexOf("\"");
            String pic=tmpstring.substring(0,pos2);
            System.out.println(pic);
            return pic;
        }

    }

    public static double getScore_v2(String res){
        JSONObject jsonObject = JSONObject.parseObject(res);
        int err_no=jsonObject.getInteger("err_no");
        if(err_no!=0){
            return -1;
        }
        else{
            JSONObject resultObject=jsonObject.getJSONObject("result");
            Double score=resultObject.getDouble("score");
            System.out.println("score is "+score);
            return score;
        }
    }

    public static String getPic_v2(String res){
        JSONObject jsonObject = JSONObject.parseObject(res);
        int err_no=jsonObject.getInteger("err_no");
        if(err_no!=0){
            return null;
        }
        else{
            JSONObject resultObject=jsonObject.getJSONObject("result");
            JSONArray jsonArray=resultObject.getJSONArray("pic_list");
            JSONObject js1=jsonArray.getJSONObject(0);
            String pic=js1.getString("pic");
            System.out.println("pic is "+pic);
            return pic;
        }
    }

    public static Double getMatchScore(String res){
        JSONObject jsonObject = JSONObject.parseObject(res);
        JSONObject resultObject=jsonObject.getJSONObject("result");
        Double score=resultObject.getDouble("score");
        return score;
    }
}
